package com.yimi.yinhepda.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.entity.response.DispatchTaskResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名称：YinHePda
 * 创建时间：2017/3/19 21:31
 * 版本号：1.0
 * 功能描述
 *
 * @author 刘宏恩
 */
public class DispatchTaskAdapter extends BaseAdapter {

    private Activity activity;
    private List<DispatchTaskResponse.DispatchTaskEntity> datas;

    public DispatchTaskAdapter(Activity activity, List<DispatchTaskResponse.DispatchTaskEntity> datas) {
        super();
        this.activity = activity;
        this.datas = datas;
    }

    /**
     * 获取当前位置信息
     */
    public int getCurrentPosition() {
        //设置其他的不选中
        for (int i = 0; i < datas.size(); i++) {
            if (datas.get(i).isChecked()) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int getCount() {
        return null != datas ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(activity).inflate(R.layout.adapter_load_task, null);
            holder = new ViewHolder();
            holder.linearLayout = convertView.findViewById(R.id.linear_top);
            holder.taskNo = convertView.findViewById(R.id.carCode);
            holder.bigArea = convertView.findViewById(R.id.next_station);
            holder.carNo = convertView.findViewById(R.id.car_number);
            holder.taskState = convertView.findViewById(R.id.totle_ticket);
            holder.radioButton = convertView.findViewById(R.id.load_task_radio_button);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //当前数据
        DispatchTaskResponse.DispatchTaskEntity item = datas.get(position);
        holder.taskNo.setText(item.getTaskNo());
        holder.bigArea.setText(item.getDispatchBigAreaName());
        holder.carNo.setText(item.getCarNo());
        switch (item.getTaskState()) {
            case 1:
                holder.taskState.setText("未配货");
                break;
            case 2:
                holder.taskState.setText("已配货");
                break;
            case 3:
                holder.taskState.setText("已确认");
                break;
            default:
                break;
        }

        if (item.isChecked()) {
            holder.radioButton.setChecked(true);
        } else {
            holder.radioButton.setChecked(false);
        }

        //一行的点击事件
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                for (DispatchTaskResponse.DispatchTaskEntity item : datas) {
                    item.setChecked(false);
                }
                datas.get(position).setChecked(true);
                notifyDataSetChanged();
            }
        });
        //radionbutton的点击事件
        holder.radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (DispatchTaskResponse.DispatchTaskEntity item : datas) {
                    item.setChecked(false);
                }
                datas.get(position).setChecked(true);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    private static class ViewHolder {
        LinearLayout linearLayout;
        /**
         * 配置单号
         */
        TextView taskNo;
        /**
         * 大区
         */
        TextView bigArea;
        /**
         * 车牌
         */
        TextView carNo;
        /**
         * 状态
         */
        TextView taskState;
        /**
         * 按钮
         */
        RadioButton radioButton;
    }
}
